package com.yitongmao.bottompop;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Administrator on 2016/10/19.
 */
public class CityUtil2 {

    private static CopyOnWriteArrayList<Cityinfo> records;
    private static List<Cityinfo> provinces;
    private static HashMap<String, List<Cityinfo>> citys;
    private static HashMap<String, List<Cityinfo>> countrys;

    public CityUtil2(Area area) {
        records = area.RECORDS;
    }
    public List<Cityinfo> initProvince() {
        if (provinces == null) {
            provinces = new ArrayList<>();
            if (records != null)
                for (int i = 0; i < records.size(); i++) {
                    if (TextUtils.equals(records.get(i).parentid, "0")) {
                        provinces.add(records.get(i));
                        records.remove(i);
                    }
                }
        }
        return provinces;
    }
    public HashMap<String, List<Cityinfo>> initCity() {
        if (citys == null) {
            citys = new HashMap<>();
            List<Cityinfo> list;
            for (int j = 0; j < provinces.size(); j++) {
                list = new ArrayList<>();
                String id = provinces.get(j).id;
                for (int i = 0; i < records.size(); i++) {
                    Cityinfo cityinfo2 = records.get(i);
                    if (TextUtils.equals(cityinfo2.parentid, id)) {
                        list.add(cityinfo2);
                        records.remove(i);
                        i--;
                    }
                }
                citys.put(id, list);
            }
        }
        return citys;
    }

    public HashMap<String, List<Cityinfo>> initCountry() {
        if (countrys == null) {
            countrys = new HashMap<>();

            Collection<List<Cityinfo>> values = citys.values();
            Iterator<List<Cityinfo>> iterator = values.iterator();
            List<Cityinfo> list;
            while (iterator.hasNext()) {
                List<Cityinfo> next = iterator.next();
                for (int j = 0; j < next.size(); j++) {
                    list = new ArrayList<>();
                    String id = next.get(j).id;
                    for (int i = 0; i < records.size(); i++) {
                        Cityinfo cityinfo2 = records.get(i);
                        if (TextUtils.equals(cityinfo2.parentid, id)) {
                            list.add(cityinfo2);
                            records.remove(i);
                            i--;
                        }
                    }
                    countrys.put(id, list);
                }
            }
        }
        return countrys;
    }

    public static List<Cityinfo> getProvinces() {
        return provinces;
    }

    public static HashMap<String, List<Cityinfo>> getCitys() {
        return citys;
    }

    public static HashMap<String, List<Cityinfo>> getCountrys() {
        return countrys;
    }
}
